package cn.lili.modules.grab.dingdong.service.impl;

import cn.lili.common.exception.ServiceException;
import cn.lili.modules.grab.dingdong.config.DingdongConfig;
import cn.lili.modules.grab.dingdong.DingdongBody;
import cn.lili.modules.grab.dingdong.base.BaseResult;
import cn.lili.modules.grab.dingdong.reps.after.*;
import cn.lili.modules.grab.dingdong.res.after.*;
import cn.lili.modules.grab.dingdong.facade.DingdongFacade;
import cn.lili.modules.grab.dingdong.service.DingdongAbsService;
import cn.lili.modules.grab.dingdong.service.DingdongAfterService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class DingdongAfterServiceImpl extends DingdongAbsService implements DingdongAfterService {

    @Resource
    private DingdongFacade dingdongFacade;
    @Resource
    private DingdongConfig dingdongConfig;

    @Override
    public List<ServiceListRespose> serviceList(ServiceListRequest serviceListRequest) {
        //构造请求
        DingdongBody dingdongBody = dingdongFacade.dingdongBodyBuilder(serviceListRequest);
        //发送请求
        BaseResult baseResult = post(dingdongConfig.getServiceListUrl(), JSONObject.toJSONString(dingdongBody), dingdongConfig.getPublic_key());
        //解析返回结果
        if ("200".equals(baseResult.getStatus())) {
            List<ServiceListRespose> resultList = JSONArray.parseArray(String.valueOf(baseResult.getContent()), ServiceListRespose.class);
            return resultList;
        }
        throw new ServiceException(baseResult.getMsg());
    }

    @Override
    public List<ServiceTypeListRespose> serviceTypeList(ServiceTypeListRequest serviceTypeListRequest) {
        //构造请求
        DingdongBody dingdongBody = dingdongFacade.dingdongBodyBuilder(serviceTypeListRequest);
        //发送请求
        BaseResult baseResult = post(dingdongConfig.getServiceTypeListUrl(), JSONObject.toJSONString(dingdongBody), dingdongConfig.getPublic_key());
        //解析返回结果
        if ("200".equals(baseResult.getStatus())) {
            List<ServiceTypeListRespose> resultList = JSONArray.parseArray(String.valueOf(baseResult.getContent()), ServiceTypeListRespose.class);
            return resultList;
        }
        throw new ServiceException(baseResult.getMsg());
    }

    @Override
    public List<CreateServiceRespose> createService(CreateServiceRequest createServiceRequest) {
        //构造请求
        DingdongBody dingdongBody = dingdongFacade.dingdongBodyBuilder(createServiceRequest);
        //发送请求
        BaseResult baseResult = post(dingdongConfig.getCreateServiceUrl(), JSONObject.toJSONString(dingdongBody), dingdongConfig.getPublic_key());
        //解析返回结果
        if ("200".equals(baseResult.getStatus())) {
            List<CreateServiceRespose> resultList = JSONArray.parseArray(String.valueOf(baseResult.getContent()), CreateServiceRespose.class);
            return resultList;
        }
        throw new ServiceException(baseResult.getMsg());
    }

    @Override
    public List<ServiceDetailRespose> serviceDetail(ServiceDetailRequest serviceDetailRequest) {
        //构造请求
        DingdongBody dingdongBody = dingdongFacade.dingdongBodyBuilder(serviceDetailRequest);
        //发送请求
        BaseResult baseResult = post(dingdongConfig.getServiceDetailUrl(), JSONObject.toJSONString(dingdongBody), dingdongConfig.getPublic_key());
        //解析返回结果
        if ("200".equals(baseResult.getStatus())) {
            List<ServiceDetailRespose> resultList = JSONArray.parseArray(String.valueOf(baseResult.getContent()), ServiceDetailRespose.class);
            return resultList;
        }
        throw new ServiceException(baseResult.getMsg());
    }

    @Override
    public List<ServiceCancelRespose> serviceCancel(ServiceCancelRequest serviceCancelRequest) {
        //构造请求
        DingdongBody dingdongBody = dingdongFacade.dingdongBodyBuilder(serviceCancelRequest);
        //发送请求
        BaseResult baseResult = post(dingdongConfig.getServiceCancelUrl(), JSONObject.toJSONString(dingdongBody), dingdongConfig.getPublic_key());
        //解析返回结果
        if ("200".equals(baseResult.getStatus())) {
            return null;
        }
        throw new ServiceException(baseResult.getMsg());
    }

    @Override
    public List<UploadResourceRespose> uploadResource(UploadResourceRequest uploadResourceRequest) {
        //构造请求
        DingdongBody dingdongBody = dingdongFacade.dingdongBodyBuilder(uploadResourceRequest);
        //发送请求
        BaseResult baseResult = post(dingdongConfig.getUploadResourceUrl(), JSONObject.toJSONString(dingdongBody), dingdongConfig.getPublic_key());
        //解析返回结果
        if ("200".equals(baseResult.getStatus())) {
            List<UploadResourceRespose> resultList = JSONArray.parseArray(String.valueOf(baseResult.getContent()), UploadResourceRespose.class);
            return resultList;
        }
        throw new ServiceException(baseResult.getMsg());
    }

    @Override
    public List<GetOrderRefundInfoRespose> getOrderRefundInfo(GetOrderRefundInfoRequest getOrderRefundInfoRequest) {
        //构造请求
        DingdongBody dingdongBody = dingdongFacade.dingdongBodyBuilder(getOrderRefundInfoRequest);
        //发送请求
        BaseResult baseResult = post(dingdongConfig.getGetOrderRefundInfoUrl(), JSONObject.toJSONString(dingdongBody), dingdongConfig.getPublic_key());
        //解析返回结果
        if ("200".equals(baseResult.getStatus())) {
            List<GetOrderRefundInfoRespose> resultList = JSONArray.parseArray(String.valueOf(baseResult.getContent()), GetOrderRefundInfoRespose.class);
            return resultList;
        }
        throw new ServiceException(baseResult.getMsg());
    }
}
